package com.lifeverse.repository;

import com.lifeverse.entity.ConsciousnessState;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 意识状态数据访问接口
 */
@Repository
public interface ConsciousnessStateRepository extends JpaRepository<ConsciousnessState, Long>, JpaSpecificationExecutor<ConsciousnessState> {
    
    /**
     * 根据生命体ID查找意识状态
     */
    List<ConsciousnessState> findByLifeEntityIdAndDeletedFalse(Long lifeEntityId);
    
    /**
     * 根据生命体ID和状态类型查找意识状态
     */
    List<ConsciousnessState> findByLifeEntityIdAndStateTypeAndDeletedFalse(Long lifeEntityId, ConsciousnessState.StateType stateType);
    
    /**
     * 根据状态类型查找意识状态
     */
    List<ConsciousnessState> findByStateTypeAndDeletedFalse(ConsciousnessState.StateType stateType);
    
    /**
     * 查找活跃的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.endTimestamp IS NULL AND cs.deleted = false")
    List<ConsciousnessState> findActiveStates();
    
    /**
     * 根据生命体ID查找活跃的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.lifeEntity.id = :lifeEntityId AND cs.endTimestamp IS NULL AND cs.deleted = false")
    List<ConsciousnessState> findActiveStatesByLifeEntity(@Param("lifeEntityId") Long lifeEntityId);
    
    /**
     * 根据强度级别范围查找意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.intensityLevel BETWEEN :minIntensity AND :maxIntensity AND cs.deleted = false")
    List<ConsciousnessState> findByIntensityLevelBetween(@Param("minIntensity") BigDecimal minIntensity, @Param("maxIntensity") BigDecimal maxIntensity);
    
    /**
     * 查找高强度的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.intensityLevel >= :minIntensity AND cs.deleted = false ORDER BY cs.intensityLevel DESC")
    Page<ConsciousnessState> findHighIntensityStates(@Param("minIntensity") BigDecimal minIntensity, Pageable pageable);
    
    /**
     * 查找异常的意识状态
     */
    List<ConsciousnessState> findByIsAnomalyTrueAndDeletedFalse();
    
    /**
     * 根据生命体ID查找异常的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.lifeEntity.id = :lifeEntityId AND cs.isAnomaly = true AND cs.deleted = false")
    List<ConsciousnessState> findAnomalousStatesByLifeEntity(@Param("lifeEntityId") Long lifeEntityId);
    
    /**
     * 查找最近的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.stateTimestamp >= :since AND cs.deleted = false ORDER BY cs.stateTimestamp DESC")
    List<ConsciousnessState> findRecentStates(@Param("since") LocalDateTime since);
    
    /**
     * 根据生命体ID查找最近的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.lifeEntity.id = :lifeEntityId AND cs.stateTimestamp >= :since AND cs.deleted = false ORDER BY cs.stateTimestamp DESC")
    List<ConsciousnessState> findRecentStatesByLifeEntity(@Param("lifeEntityId") Long lifeEntityId, @Param("since") LocalDateTime since);
    
    /**
     * 查找长时间持续的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.durationSeconds > :minDuration AND cs.deleted = false ORDER BY cs.durationSeconds DESC")
    List<ConsciousnessState> findLongDurationStates(@Param("minDuration") Long minDuration);
    
    /**
     * 查找高质量的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.qualityScore >= :minQuality AND cs.deleted = false ORDER BY cs.qualityScore DESC")
    Page<ConsciousnessState> findHighQualityStates(@Param("minQuality") BigDecimal minQuality, Pageable pageable);
    
    /**
     * 根据认知负载范围查找意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.cognitiveLoad BETWEEN :minLoad AND :maxLoad AND cs.deleted = false")
    List<ConsciousnessState> findByCognitiveLoadBetween(@Param("minLoad") BigDecimal minLoad, @Param("maxLoad") BigDecimal maxLoad);
    
    /**
     * 查找高创造力的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.creativityIndex >= :minCreativity AND cs.deleted = false ORDER BY cs.creativityIndex DESC")
    List<ConsciousnessState> findHighCreativityStates(@Param("minCreativity") BigDecimal minCreativity);
    
    /**
     * 查找高逻辑清晰度的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.logicClarity >= :minClarity AND cs.deleted = false ORDER BY cs.logicClarity DESC")
    List<ConsciousnessState> findHighLogicClarityStates(@Param("minClarity") BigDecimal minClarity);
    
    /**
     * 查找高直觉敏感度的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.intuitionSensitivity >= :minSensitivity AND cs.deleted = false ORDER BY cs.intuitionSensitivity DESC")
    List<ConsciousnessState> findHighIntuitionStates(@Param("minSensitivity") BigDecimal minSensitivity);
    
    /**
     * 查找高社交活跃度的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.socialActivity >= :minActivity AND cs.deleted = false ORDER BY cs.socialActivity DESC")
    List<ConsciousnessState> findHighSocialActivityStates(@Param("minActivity") BigDecimal minActivity);
    
    /**
     * 查找高学习效率的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.learningEfficiency >= :minEfficiency AND cs.deleted = false ORDER BY cs.learningEfficiency DESC")
    List<ConsciousnessState> findHighLearningEfficiencyStates(@Param("minEfficiency") BigDecimal minEfficiency);
    
    /**
     * 统计各状态类型的数量
     */
    @Query("SELECT cs.stateType, COUNT(cs) FROM ConsciousnessState cs WHERE cs.deleted = false GROUP BY cs.stateType")
    List<Object[]> countByStateType();
    
    /**
     * 根据生命体ID统计各状态类型的数量
     */
    @Query("SELECT cs.stateType, COUNT(cs) FROM ConsciousnessState cs WHERE cs.lifeEntity.id = :lifeEntityId AND cs.deleted = false GROUP BY cs.stateType")
    List<Object[]> countByStateTypeAndLifeEntity(@Param("lifeEntityId") Long lifeEntityId);
    
    /**
     * 获取平均强度级别
     */
    @Query("SELECT AVG(cs.intensityLevel) FROM ConsciousnessState cs WHERE cs.deleted = false")
    BigDecimal getAverageIntensityLevel();
    
    /**
     * 获取平均质量评分
     */
    @Query("SELECT AVG(cs.qualityScore) FROM ConsciousnessState cs WHERE cs.deleted = false")
    BigDecimal getAverageQualityScore();
    
    /**
     * 获取平均认知负载
     */
    @Query("SELECT AVG(cs.cognitiveLoad) FROM ConsciousnessState cs WHERE cs.deleted = false")
    BigDecimal getAverageCognitiveLoad();
    
    /**
     * 根据生命体ID获取最新的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.lifeEntity.id = :lifeEntityId AND cs.deleted = false ORDER BY cs.stateTimestamp DESC")
    Page<ConsciousnessState> findLatestStatesByLifeEntity(@Param("lifeEntityId") Long lifeEntityId, Pageable pageable);
    
    /**
     * 查找需要关注的意识状态（异常或低质量）
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE (cs.isAnomaly = true OR cs.qualityScore < :qualityThreshold) AND cs.deleted = false")
    List<ConsciousnessState> findStatesNeedingAttention(@Param("qualityThreshold") BigDecimal qualityThreshold);
    
    /**
     * 根据触发因素查找意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.triggerFactor LIKE %:keyword% AND cs.deleted = false")
    List<ConsciousnessState> findByTriggerFactorContaining(@Param("keyword") String keyword);
    
    /**
     * 查找特定时间段内的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.stateTimestamp BETWEEN :startTime AND :endTime AND cs.deleted = false ORDER BY cs.stateTimestamp")
    List<ConsciousnessState> findByTimePeriod(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
    
    /**
     * 根据生命体ID查找特定时间段内的意识状态
     */
    @Query("SELECT cs FROM ConsciousnessState cs WHERE cs.lifeEntity.id = :lifeEntityId AND cs.stateTimestamp BETWEEN :startTime AND :endTime AND cs.deleted = false ORDER BY cs.stateTimestamp")
    List<ConsciousnessState> findByLifeEntityAndTimePeriod(@Param("lifeEntityId") Long lifeEntityId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
}